package cn.stylefeng.guns.modular.basic.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.CangGuanRoleId;
import cn.stylefeng.guns.modular.basic.entity.BasicCarrier;
import cn.stylefeng.guns.modular.basic.entity.BasicDriver;
import cn.stylefeng.guns.modular.basic.entity.BasicOperate;
import cn.stylefeng.guns.modular.basic.model.params.BasicAreaParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicCarrierParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicOilCardParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicAreaResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicCarrierResult;
import cn.stylefeng.guns.modular.basic.service.BasicCarrierService;
import cn.stylefeng.guns.modular.basic.service.BasicOilCardService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.sys.modular.system.entity.Role;
import cn.stylefeng.guns.sys.modular.system.service.RoleService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.model.UserDto;
import cn.stylefeng.guns.sys.modular.system.service.UserService;

import java.util.List;

import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 承运商档案控制器
 *
 * @author zhucx
 * @Date 2022-12-29 11:40:34
 */
@Controller
@RequestMapping("/basicCarrier")
public class BasicCarrierController extends BaseController {

    private String PREFIX = "/basic/basicCarrier";

    @Autowired
    private BasicCarrierService basicCarrierService;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private BasicOperateService basicOperateService;

    @Autowired
    private BasicOilCardService basicOilCardService;

    /**
     * 跳转到主页面
     *
     * @author zhucx
     * @Date 2022-12-29
     */
    @RequestMapping("")
    public String index(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicCarrier.html";
    }

    /**
     * 新增页面
     *
     * @author zhucx
     * @Date 2022-12-29
     */
    @RequestMapping("/add")
    public String add(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择油卡
    	BasicOilCardParam basicOilCardParam = new BasicOilCardParam();
    	basicOilCardParam.setDeliverId(deliverId);
    	model.addAttribute("basicOilCardlist", basicOilCardService.findListBySpec(basicOilCardParam,user.getOperateId()));
    	
        return PREFIX + "/basicCarrier_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhucx
     * @Date 2022-12-29
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择油卡
    	BasicOilCardParam basicOilCardParam = new BasicOilCardParam();
    	basicOilCardParam.setDeliverId(deliverId);
    	model.addAttribute("basicOilCardlist", basicOilCardService.findListBySpec(basicOilCardParam,user.getOperateId()));
        return PREFIX + "/basicCarrier_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhucx
     * @Date 2022-12-29
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BasicCarrierParam basicCarrierParam) {
    	User user = userService.getcurrentUser();
    	basicCarrierParam.setCreateUser(user.getName());
    	
    	Object warehouseOperateId = basicCarrierParam.getOperateId();
    	if (warehouseOperateId!=null) {
    		BasicOperate Operate = basicOperateService.getById(Integer.parseInt(warehouseOperateId.toString()));
    		basicCarrierParam.setOperateName(Operate.getOperateName());
        	basicCarrierParam.setDeliverId(Operate.getDeliverId());
        	basicCarrierParam.setDeptId(Operate.getDeptId());
		}
    	
    	BasicCarrierParam check = new BasicCarrierParam();
    	check.setCarrierName(basicCarrierParam.getCarrierName());
    	check.setDeliverId(user.getDeliverId());
    	BasicCarrierResult r = basicCarrierService.findBySpec(check,user.getOperateId());
    	if(r != null) {
    		return ResponseData.error(basicCarrierParam.getCarrierName()+"Already Exists");
    	}
    	basicCarrierParam.setV1("1");
    	//账号
        if(ToolUtil.isNotEmpty(basicCarrierParam.getCarrierAccount())){
            User byAccount = userService.getByAccount(basicCarrierParam.getCarrierAccount());
            if(byAccount == null){
                return ResponseData.error(basicCarrierParam.getCarrierAccount()+"Account Does Not Exist");
            }else{
                basicCarrierParam.setUserId(byAccount.getUserId());
            }
        }
        this.basicCarrierService.add(basicCarrierParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhucx
     * @Date 2022-12-29
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BasicCarrierParam basicCarrierParam) {
    	basicCarrierParam.setUpdateUser(userService.getcurrentUser().getName());
    	Object warehouseOperateId = basicCarrierParam.getOperateId();
    	if (warehouseOperateId!=null) {
    		BasicOperate Operate = basicOperateService.getById(Integer.parseInt(warehouseOperateId.toString()));
    		basicCarrierParam.setOperateName(Operate.getOperateName());
		}
        //账号
        if(ToolUtil.isNotEmpty(basicCarrierParam.getCarrierAccount())){
            User byAccount = userService.getByAccount(basicCarrierParam.getCarrierAccount());
            if(byAccount == null){
                return ResponseData.error(basicCarrierParam.getCarrierAccount()+"Account Does Not Exist");
            }else{
                basicCarrierParam.setUserId(byAccount.getUserId());
            }
        }
        this.basicCarrierService.update(basicCarrierParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     * @author zhucx
     * @Date 2022-12-29
     */
    @RequestMapping("/disable")
    @ResponseBody
    public ResponseData disable(BasicCarrierParam basicCarrierParam) {
    	basicCarrierParam.setUpdateUser(userService.getcurrentUser().getName());
        this.basicCarrierService.update(basicCarrierParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhucx
     * @Date 2022-12-29
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BasicCarrierParam basicCarrierParam) {
        this.basicCarrierService.delete(basicCarrierParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhucx
     * @Date 2022-12-29
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BasicCarrierParam basicCarrierParam) {
        BasicCarrier detail = this.basicCarrierService.getById(basicCarrierParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     * @author zhucx
     * @Date 2019-07-01
     */
    @ResponseBody
    @RequestMapping("/Querylist")
    public List<BasicCarrierResult> Querylist(BasicCarrierParam basicCarrierParam) {
    	User user = userService.getcurrentUser();
    	basicCarrierParam.setDeptId(user.getDeptId());

        String roleId = user.getRoleId();
        if(ToolUtil.isNotEmpty(roleId)){
            for(String rId : roleId.split(",")){
                Role byId = roleService.getById(rId);
                if(byId != null && byId.getName().contains("Carrier")){
                    basicCarrierParam.setCarrierAccount(user.getAccount());
                }
            }
        }
    	return this.basicCarrierService.findListBySpec(basicCarrierParam, user.getOperateId());
    }

    /**
     * 查询列表
     *
     * @author zhucx
     * @Date 2022-12-29
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BasicCarrierParam basicCarrierParam,String condition ) {
    	User user = userService.getcurrentUser();
    	basicCarrierParam.setDeliverId(user.getDeliverId());

    	// 默认部门
        basicCarrierParam.setDeptId(user.getDeptId());

        String roleId = user.getRoleId();
        if(ToolUtil.isNotEmpty(roleId)){
            for(String rId : roleId.split(",")){
                Role byId = roleService.getById(rId);
                if(byId != null && byId.getName().contains("Carrier")){
                    basicCarrierParam.setCarrierAccount(user.getAccount());
                }
            }
        }
        return this.basicCarrierService.findPageBySpec(basicCarrierParam,condition,user.getOperateId());
    }

}


